package vehimania
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;

	import mapkit.MapHandler;

	[Event(name="vehimaniaAhead", type="vehimania.VehicleControlEvent")]
	[Event(name="vehimaniaBack", type="vehimania.VehicleControlEvent")]
	[Event(name="vehimaniaLeft", type="vehimania.VehicleControlEvent")]
	[Event(name="vehimaniaRight", type="vehimania.VehicleControlEvent")]
	[Event(name="vehimaniaStop", type="vehimania.VehicleControlEvent")]
	[Event(name="vehimaniaSelect", type="vehimania.VehicleControlEvent")]
	public class VehicleControl extends MapHandler
	{
		override public function get keyboardLocked():Boolean
		{
			return true;
		}

		override public function get mouseLocked():Boolean
		{
			return false;
		}

		override protected function load():void
		{
			super.load();
			setKeyboardFocus();
			map.addEventListener(Event.ENTER_FRAME, setKeyboardFocus);
			map.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			map.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
		}

		override protected function unload():void
		{
			map.removeEventListener(Event.ENTER_FRAME, setKeyboardFocus);
			map.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			map.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			super.unload();
		}

		protected function setKeyboardFocus(event:Event=null):void
		{
			map.layers.setFocus();
		}

		protected function keyDownHandler(event:KeyboardEvent):void
		{
			var action:int=0;
			switch (event.keyCode)
			{
				case Keyboard.UP:
					dispatchEvent(new VehicleControlEvent(VehicleControlEvent.AHEAD,
						map, event));
					break;
				case Keyboard.DOWN:
					dispatchEvent(new VehicleControlEvent(VehicleControlEvent.BACK,
						map, event));
					break;
				case Keyboard.LEFT:
					dispatchEvent(new VehicleControlEvent(VehicleControlEvent.LEFT,
						map, event));
					break;
				case Keyboard.RIGHT:
					dispatchEvent(new VehicleControlEvent(VehicleControlEvent.RIGHT,
						map, event));
					break;
			}
			switch (String.fromCharCode(event.charCode))
			{
				case "W":
				case "w":
					dispatchEvent(new VehicleControlEvent(VehicleControlEvent.AHEAD,
						map, event));
					break;
				case "S":
				case "s":
					dispatchEvent(new VehicleControlEvent(VehicleControlEvent.BACK,
						map, event));
					break;
				case "A":
				case "a":
					dispatchEvent(new VehicleControlEvent(VehicleControlEvent.LEFT,
						map, event));
					break;
				case "D":
				case "d":
					dispatchEvent(new VehicleControlEvent(VehicleControlEvent.RIGHT,
						map, event));
					break;
				case "B":
				case "b":
					dispatchEvent(new VehicleControlEvent(VehicleControlEvent.SELECT,
						map, event));
					break;
			}
		}

		protected function keyUpHandler(event:KeyboardEvent):void
		{
			dispatchEvent(new VehicleControlEvent(VehicleControlEvent.STOP, map,
				event));
		}
	}
}